/**
 * Created by fq on 2018/1/23 0023.
 */
'use strict';
angular.module('indexApp')
    .controller('apply_activityCtrl', function ($scope, $state, $rootScope, $http, $stateParams, $timeout, $interval) {
        var vm = this;
        vm.http = $rootScope.http;
        vm.alert = false;
        $scope.state = $state;
        $(document).ready(function () {
            $("#dtBox").DateTimePicker(
                {
                    dateFormat: "yyyy-MM-dd",
                });
        });
        vm.alert = false;
        //发送验证码

        //发送验证码
        //倒计时
        vm.timer = false;
        vm.timeout = 120000;
        vm.timerCount = vm.timeout / 1000;
        vm.text = "获取验证码";
        vm.sendSms = function (num) {
            // $rootScope.alert($rootScope.id);
            // $rootScope.alert(num);
            if (num == '' || num == undefined || num == null) {
                $rootScope.alert('请填写正确的手机号')
            }
            else {
                $http({
                    method: 'post',
                    url: vm.http + 'send_sms',
                    data: {
                        mobile: num,
                        user_id: $rootScope.id
                    }
                })
                    .then(function successCallback(res) {
                        if (res.data.code == 0) {
                            // $rootScope.alert(res.data.msg);
                            // vm.data.code = res.data.data.code;
                            vm.showTimer = true;
                            vm.timer = true;
                            vm.text = "秒后重新获取";
                            var counter = $interval(function () {
                                vm.timerCount = vm.timerCount - 1;
                            }, 1000);
                            $timeout(function () {
                                vm.text = "获取验证码";
                                vm.timer = false;
                                $interval.cancel(counter);
                                vm.showTimer = false;
                                vm.timerCount = vm.timeout / 1000;
                            }, vm.timeout);
                        }
                        else {
                            $rootScope.alert(res.data.msg)
                        }
                    }, function errorCallback(res) {// 请求失败执行代码
                        $rootScope.alertError();
                    });
            }

        };


        vm.registered = function () {
            vm.data.activity_id = $stateParams.id;
            vm.data.user_id = $rootScope.id;
            $http({
                method: 'post',
                url: vm.http + 'submit_activity',
                data: vm.data
            })
                .then(function successCallback(res) {

                    if (res.data.code == 0) {
                        vm.alert = !vm.alert
                    }
                    else {
                        $rootScope.alert(res.data.msg)
                    }

                }, function errorCallback(res) {// 请求失败执行代码
                    $rootScope.alertError()
                });
        }


    });